home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7312 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  82 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Turbo C 3.0 - arithmetic in defines
  5. Date: 13 Feb 1996 20:13:32 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb13131332@qcd.lanl.gov>
  8. References: <4fqgkf$iqd@ccshst05.cs.uoguelph.ca>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: thay@uoguelph.ca's message of 13 Feb 1996 17:09:35 GMT
  13.  
  14. In article <4fqgkf$iqd@ccshst05.cs.uoguelph.ca> thay@uoguelph.ca (Toby
  15. K Hay) writes: 
  16. <snip>
  17.    As a newcomer to C programming I'm having difficulty using arithmetic
  18.    and #defined values to dimension arrays at compile time.  The following 
  19.    very short program reproduces my error:
  20.  
  21. Your error is very common misunderstanding amongst newcomers. I will
  22. try to explain what I think is your problem. 
  23.  
  24.    #define var1 16
  25.    #define var2 485
  26.    #define var3 ((var2-1)/var1)+1
  27.    int main(void)
  28.        {
  29.        printf("\n%i    %i    %i    %i",var1,var2,var3,var1*var3);
  30.        return 0;
  31.        }
  32.  
  33.    Output from this is '16 485 31 481', not '16 485 31 496' as I should have 
  34.    expected.  That is, var3 is calculated correctly in its #define, but the 
  35.  
  36. This is precisely your problem. A #define _never_ calculates
  37. anythings: it simply defines a way of dumping in a sequence of tokens
  38. when you need it. What the first define says is that when you later
  39. use the token var1 in your code, and it is time for expanding the
  40. token, it should be replaced by the token 16 instead. Thus, if you
  41. wrote x+var1, the compiler would see token x, token +, token var1, and
  42. then say "Aha! var1 is a `macro'!", and so replace var1 with 16. Then
  43. it will say "Good! There is nothing more relevant to expand, so I have
  44. found the three tokens x, + and 16, which means that the programmer
  45. wants me to generate code for adding 16 to whatever x is." var2 is
  46. similar.
  47.  
  48. Now see what happens when it sees var3? It says I have the sequence of
  49. 11 tokens on replacement: viz. ( ( var2 - 1 ) / ( var1 ) + 1, where I
  50. have used spaces to show the separation between the tokens. Now, it
  51. goes back and looks at the sequence and recognizes var2 and var1 as
  52. macros, and replaces them further: ending up with ( ( 485 - 1 ) / ( 16
  53. ) + 1. It is only at this stage does it start interpreting this
  54. expression: and either calculates it or compiles instruction for
  55. calculating it. The result is of course 31.
  56.  
  57. So what happens when you say var1*var3? Well, if you follow through
  58. the same argument, you find that you end up with 16*((485-1)/16)+1
  59. (this time I amassuming you know what the tokens are, so I am not
  60. showing the extraneous spaces that I was showing earlier) which it
  61. then interprets, getting a 481, naturally.
  62.  
  63.    var1*var3 in the printf() statement is not calculated correctly.  Where 
  64.    am I going wrong?
  65.  
  66. Is it clear now? Remember to always define macros with an extra set of
  67. parentheses whenever possible, unless they are one token long. Thus
  68. #define var1 16 is fine (one token long),
  69. #define complicated do { x:= 1; } while(0) is also fine (parentheses
  70. not allowed), but #define var3 (((var2-1)/var1)+1) is what you ought
  71. to do.
  72.  
  73. Cheers
  74. Tanmoy
  75. --
  76. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  77. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  78. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  79. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  80. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  81. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  82.